package sopt.issueChaser;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import comm.util.CommUtil;
import comm.util.SoptHashMap;
 

public class Metawebblog {

    public static final String SERVER_URL = "http://cyhome.cyworld.com/a0334516/api";
    public static final String userid = "a0334516";
    public static final String password = "4b56093ed7dae";
    public static final String blogid = "1";

    public static void main(String[] args){

        XmlRpcClientConfigImpl xmlRpcConfig = new XmlRpcClientConfigImpl();
        
        try {
			xmlRpcConfig.setServerURL(new URL(SERVER_URL));
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			System.out.println("###셋 서버 에라~");
			e1.printStackTrace();
		}

        XmlRpcClient xmlRpcClient = new XmlRpcClient();
        
        xmlRpcClient.setConfig(xmlRpcConfig);

        // 카테고리 목록 가져오기
        Object[] params = new Object[] { blogid, userid, password };
        
        
        Object[] returnedObj = null;
		try {
			returnedObj = (Object[]) xmlRpcClient.execute(
			        "metaWeblog.getCategories", params);
		} catch (XmlRpcException e) {
			// TODO Auto-generated catch block
			System.out.println("###카테고리 에라~~~");
			e.printStackTrace();
		}
        for (Object o : returnedObj) {
            System.out.println(" Type : " + o.getClass().getCanonicalName());

            Map map = (Map) o;
            for (Object key : map.keySet()) {
                System.out.println("        - " + key + " : " + map.get(key));
            }
        }
        


//        // 최근 포스트 가져오기
//        params = new Object[] { 1, userid, password, 10 };
//        returnedObj = (Object[]) xmlRpcClient.execute(
//                "metaWeblog.getRecentPosts", params);
//        for (Object o : returnedObj) {
//            System.out.println(" Type : " + o.getClass().getCanonicalName());
//
//            Map map = (Map) o;
//            for (Object key : map.keySet()) {
//                System.out.println("        - " + key + " : " + map.get(key));
//            }
//        }
        
        // 포스팅
        Map<String, String> m = new HashMap<String, String>();
        String title = "11111111111111ㅁㅁㅁㄱㄱㄱㄱㄱㄱㄱㄱㄱ제목 되냐~~~";
        String description = "좀 더 길게 써볼까-ㅁ-;; ㅋㅋㅋㅋㅋㅋㅋ" +
		"내용 고고고싱!+ㅁ+ 와우!!!<a href='http://auctionmaster.cafe24.com/engSearch'>ESKorea</a>\r\n와우!! ㅋㅋㅋㅋ";

        
        //글 제목
        m.put("title", title);
                
        //본문
        m.put("description", description);
        //카테고리 설정 (넘어온 리스트에 있는 글자로 설정);
        m.put("categories", "Changdoc's Tobacco Story...");
        m.put("mt_keywords", "담배,tobacco,ㅋㅋㅋㅋ,");
        params = new Object[]{blogid, userid, password, m, true};
        
//        returnedObj = (Object[]) xmlRpcClient.execute(
//                "metaWeblog.newPost", params);
        
        String result = null;
		try {
			result = xmlRpcClient.execute(
			        "metaWeblog.newPost", params).toString();
		} catch (XmlRpcException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//        이글루스는 저장한 글번호 리턴
        System.out.println("포스트 결과!+_+:"+result);
        


    }
}
